@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using BlazorApp.Pages.Event
@using Extension
@using k8s.Models
@using Mapster
@inherits BlazorApp.Pages.Common.DrawerPageBase<V1CustomResourceDefinition>
@if (CustomResourceDefinition != null)
{
    <CrdAction Item="CustomResourceDefinition" MenuMode="MenuMode.Horizontal"></CrdAction>
    <Divider Orientation="left" Style="font-weight:bold">@L["Metadata"]</Divider>
    <MetadataView Item="CustomResourceDefinition.Metadata" ExplainFieldPrefix="ingress"></MetadataView>
    <Divider Orientation="left" Style="font-weight:bold">@L["Spec"]</Divider>
    <HTable Bordered Column="1">
        <HTableColumn Title=@L["Conversion"] ExplainField="crd.spec.conversion">
            <HTable>
                <PropertySimpleView Title=@L["strategy"] Item="CustomResourceDefinition.Spec.Conversion.Strategy"
                                    ExplainField="crd.spec.conversion.strategy">
                </PropertySimpleView>
                @if (CustomResourceDefinition.Spec.Conversion.Webhook != null)
                {
                    <PropertySimpleView Title=@L["WebHook"] Item="@CustomResourceDefinition.Spec.Conversion.Webhook"
                                        ExplainField="crd.spec.conversion.webhook" ShowInJson="true">
                    </PropertySimpleView>
                }
            </HTable>
        </HTableColumn>
        <PropertySimpleView Title=@L["Group"] Item="CustomResourceDefinition.Spec.Group"
                            ExplainField="crd.spec.group">
        </PropertySimpleView>
        <PropertySimpleView Title=@L["Scope"] Item="CustomResourceDefinition.Spec.Scope"
                            ExplainField="crd.spec.scope">
        </PropertySimpleView>
        @if (CustomResourceDefinition.Spec.Names != null)
        {
            <HTableColumn Title=@L["Names"] ExplainField="crd.spec.names">
                <HTable>
                    <PropertyListView Title=@L["Categories"] T="string"
                                      Items="CustomResourceDefinition.Spec.Names.Categories"
                                      ExplainField="crd.spec.names.categories">
                    </PropertyListView>
                    <PropertySimpleView Title=@L["Kind"] Item="CustomResourceDefinition.Spec.Names.Kind"
                                        ExplainField="crd.spec.names.kind">
                    </PropertySimpleView>
                    <PropertySimpleView Title=@L["ListKind"] Item="CustomResourceDefinition.Spec.Names.ListKind"
                                        ExplainField="crd.spec.names.listKind">
                    </PropertySimpleView>
                    <PropertySimpleView Title=@L["Plural"] Item="CustomResourceDefinition.Spec.Names.Plural"
                                        ExplainField="crd.spec.names.plural">
                    </PropertySimpleView>
                    <PropertyListView Title=@L["ShortNames"] T="string"
                                      Items="CustomResourceDefinition.Spec.Names.ShortNames"
                                      ExplainField="crd.spec.names.shortNames">
                    </PropertyListView>
                    <PropertySimpleView Title=@L["Singular"] Item="CustomResourceDefinition.Spec.Names.Singular"
                                        ExplainField="crd.spec.names.singular">
                    </PropertySimpleView>
                </HTable>
            </HTableColumn>
        }
        <PropertySimpleView Title=@L["PreserveUnknownFields"] Item="CustomResourceDefinition.Spec.PreserveUnknownFields"
                            ExplainField="crd.spec.preserveUnknownFields">
        </PropertySimpleView>
        @if (CustomResourceDefinition.Spec.Versions is { Count: > 0 })
        {
            <HTableColumn Title=@L["Versions"] ExplainField="crd.spec.versions">

                @foreach (var version in CustomResourceDefinition.Spec.Versions)
                {
                    <HTable >
                        <PropertySimpleView Title=@L["Name"] Item="version.Name" ExplainField="crd.spec.versions.name"></PropertySimpleView>
                        <PropertySimpleView Title=@L["Served"] Item="version.Served"
                                            ExplainField="crd.spec.versions.served">
                        </PropertySimpleView>
                        <PropertySimpleView Title=@L["Storage"] Item="version.Storage"
                                            ExplainField="crd.spec.versions.storage">
                        </PropertySimpleView>
                        <PropertySimpleView Title=@L["Deprecated"] Item="version.Deprecated"
                                            ExplainField="crd.spec.versions.deprecated">
                        </PropertySimpleView>
                        <PropertySimpleView Title=@L["DeprecationWarning"] Item="version.DeprecationWarning"
                                            ExplainField="crd.spec.versions.deprecationWarning"
                                            ShowInJson="true">
                        </PropertySimpleView>
                        @if (version.Subresources != null)
                        {
                            <HTableColumn Title=@L["SubResources"]>
                                <HTable>
                                    <PropertySimpleView Title=@L["Status"] Item="version.Subresources.Status" ExplainField="crd.spec.versions.subresources.status" ShowInJson="true"></PropertySimpleView>
                                    @if (version.Subresources.Scale != null)
                                    {
                                        if (version.Subresources.Scale.LabelSelectorPath != null)
                                        {
                                            <ColorfulTag > @($"Scale.Label=: {version.Subresources.Scale.LabelSelectorPath}")</ColorfulTag>
                                        }

                                        if (version.Subresources.Scale.SpecReplicasPath != null)
                                        {
                                            <ColorfulTag > @($"Scale.SpecReplicas=: {version.Subresources.Scale.SpecReplicasPath}")</ColorfulTag>
                                        }

                                        if (version.Subresources.Scale.StatusReplicasPath != null)
                                        {
                                            <ColorfulTag > @($"Scale.StatusReplicas=: {version.Subresources.Scale.StatusReplicasPath}")</ColorfulTag>
                                        }
                                    }
                                </HTable>
                            </HTableColumn>
                            <PropertySimpleView Title=@L["Status"] Item="version.Subresources.Status" ExplainField="crd.spec.versions.subresources.status" ShowInJson="true"></PropertySimpleView>
                        }

                        @if (version.AdditionalPrinterColumns is { Count: > 0 })
                        {
                            <HTableColumn Title=@L["AdditionalPrinterColumns"]>

                                @foreach (var col in version.AdditionalPrinterColumns)
                                {
                                    <HTable>
                                        <PropertySimpleView Title=@L["Name"] Item="col.Name" ExplainField="crd.spec.versions.printerColumns.name"></PropertySimpleView>
                                        <PropertySimpleView Title=@L["Priority"] Item="col.Priority"
                                                            ExplainField="crd.spec.versions.printerColumns.priority">
                                        </PropertySimpleView>
                                        <PropertySimpleView Title=@L["Type"] Item="col.Type" ExplainField="crd.spec.versions.printerColumns.type"></PropertySimpleView>
                                        <PropertySimpleView Title=@L["JsonPath"] Item="col.JsonPath"
                                                            ExplainField="crd.spec.versions.printerColumns.jsonPath">
                                        </PropertySimpleView>
                                        <PropertySimpleView Title=@L["Format"] Item="col.Format"
                                                            ExplainField="crd.spec.versions.printerColumns.format">
                                        </PropertySimpleView>
                                        <PropertySimpleView Title=@L["Description"] Item="col.Description"
                                                            ExplainField="crd.spec.versions.printerColumns.description"
                                                            ShowInJson="true">
                                        </PropertySimpleView>
                                    </HTable>
                                }

                            </HTableColumn>
                        }
                    </HTable>
                }


            </HTableColumn>
        }

    </HTable>
    <Divider Orientation="left" Style="font-weight:bold">@L["CR Instance"]</Divider>
    <HTable Bordered Column="1">
        <HTr>
            <HTh>@L["Kind"]</HTh>
            <HTh>@L["Version"]</HTh>
            <HTh>@L["Name"]</HTh>
            <HTh>@L["Age"]</HTh>
        </HTr>

        @if (_crInstanceList.Count == 0)
        {
            <HTr>
                <HTd ColSpan="4">
                    <Empty Simple/>
                </HTd>

            </HTr>
        }
        @foreach (var cr in _crInstanceList)
        {
            <HTr>
                <HTd>@cr.Kind</HTd>
                <HTd>@cr.ApiVersion</HTd>
                <HTd>
                    <Button OnClick="@(() => OnYamlClick(cr))" Type="@ButtonType.Link">@cr.Name()</Button>
                </HTd>
                <HTd>@cr.CreationTimestamp().AgeFromUtc()</HTd>
            </HTr>
        }

    </HTable>

    <Divider Orientation="left" Style="font-weight:bold">@L["Status"]</Divider>
    <HTable Bordered Column="1">
        <ConditionsView Conditions="@(CustomResourceDefinition.Status.Conditions.Adapt<IList<V1Condition>>())" ExplainField="crd.status.conditions"></ConditionsView>
        @if (CustomResourceDefinition.Status.AcceptedNames != null)
        {
            <HTableColumn Title=@L["AcceptedNames"] ExplainField="crd.status.acceptedNames">
                <HTable>
                    <PropertyListView Title=@L["Categories"] T="string"
                                      Items="CustomResourceDefinition.Status.AcceptedNames.Categories"
                                      ExplainField="crd.status.acceptedNames.categories">
                    </PropertyListView>
                    <PropertySimpleView Title=@L["Kind"] Item="CustomResourceDefinition.Status.AcceptedNames.Kind"
                                        ExplainField="crd.status.acceptedNames.kind">
                    </PropertySimpleView>
                    <PropertySimpleView Title=@L["ListKind"]
                                        Item="CustomResourceDefinition.Status.AcceptedNames.ListKind"
                                        ExplainField="crd.status.acceptedNames.listKind">
                    </PropertySimpleView>
                    <PropertySimpleView Title=@L["Plural"] Item="CustomResourceDefinition.Status.AcceptedNames.Plural"
                                        ExplainField="crd.status.acceptedNames.plural">
                    </PropertySimpleView>
                    <PropertyListView Title=@L["ShortNames"] T="string"
                                      Items="CustomResourceDefinition.Status.AcceptedNames.ShortNames"
                                      ExplainField="crd.status.acceptedNames.shortNames">
                    </PropertyListView>
                    <PropertySimpleView Title=@L["Singular"]
                                        Item="CustomResourceDefinition.Status.AcceptedNames.Singular"
                                        ExplainField="crd.status.acceptedNames.singular">
                    </PropertySimpleView>
                </HTable>
            </HTableColumn>
        }
        <PropertyListView Title=@L["StoredVersions"] T="string" Items="@CustomResourceDefinition.Status.StoredVersions"
                          ExplainField="crd.status.storedVersions">
        </PropertyListView>
    </HTable>
    <MiniEventListView Uid="@CustomResourceDefinition.Metadata.Uid"></MiniEventListView>
}
